import { reqGoodDetail,reqAddOrUpdateShopCart } from "@/api";
import { getUUID } from "@/utils/uuid_token";
const actions = {
  // 获取商品详情
  async getGoodDetail({commit},skuId){
    try {
      let res = await reqGoodDetail(skuId);
      if(res.code == 200){
        commit('GOOD_DETAIL',res.data);
      }else {
        return Promise.reject(new Error(res.message));
      }
    } catch (error) {
      alert(error.message);
    }
  },
  async addShopCart({commit},{skuId,skuNum}){
    let res = await reqAddOrUpdateShopCart(skuId,skuNum);
    if(res.code==200){
      return '成功添加购物车';
    }else {
      return Promise.reject(new Error(res.message));
    }
  },
};
const mutations = {
  GOOD_DETAIL(state,goodDetail){
    state.goodDetail = goodDetail;
  },
};
const state = {
  goodDetail:{},
  // 生成一个随机的字符串，一旦生成不可更改
  uuid_token:getUUID(),
};
const getters = {
  categoryView(state){
    // 如果数据还没回来undefined.category1Name会报错，所以必须加上{};
    return state.goodDetail.categoryView || {};
  },
  skuInfo(state){
    return state.goodDetail.skuInfo || {};
  },
  spuSaleAttrList(state){
    return state.goodDetail.spuSaleAttrList || [];
  },
};
export default {
  namespaced:true,
  actions,
  mutations,
  state,
  getters,
}